/*
 * @Author: tcosfish
 * @Date: 2022-07-21 09:08:36
 * @LastEditors: tcosfish
 * @LastEditTime: 2022-08-18 15:26:00
 * @FilePath: \loadFile\src\app.ts
 */

import Koa from "koa"

import cors from "@/middlewares/cors"
import body from "@/middlewares/body"
import * as routes from "@/middlewares/routes"
import serve_config from "@/config/serve.config"

import { getLocalIP } from "@/utils/getLocalIP"

const app = new Koa()

app.use(cors)
app.use(body)

type routes_key_type = keyof typeof routes
// 动态注册中间件导出的路由
for (const key in routes) {
  app
    .use(routes[key as routes_key_type].routes())
    .use(routes[key as routes_key_type].allowedMethods())
}

app.listen(serve_config.port, () => {
  const address = getLocalIP()
  console.log(`serve running: http://${address}:${serve_config.port}`)
})
